package com.angel.controller;

import com.angel.bean.Branches;
import com.angel.bean.VipCard;
import com.angel.dto.BranchesDto;
import com.angel.dtoutlis.VipCardDtoUtil;
import com.angel.exception.StatusException;
import com.angel.service.BranchesService;
import com.angel.service.OtherlogService;
import com.angel.service.VipCardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/vipCardController")
public class VipCardController {

   @Resource
   private VipCardService vipCardService;

   @Resource
   private BranchesService branchesService;
    @Autowired
    private OtherlogService otherlogService;


    /**
     * 添加
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/addVipCard")
    @ResponseBody
    public Object addVipCard(VipCard vipCard) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        vipCard.setState(1);
        int result = vipCardService.insert(vipCard);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }


    /***
     * 更新
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @param vipCard 前端传入id值
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/updateVipCard")
    @ResponseBody
    public Object updateVipCard(VipCard vipCard) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = vipCardService.update(vipCard);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /***
     * 删除
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @param vipCard 前端传入emId值
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/deleteVipCard")
    @ResponseBody
    public Object deleteVipCard(VipCard vipCard) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        vipCard.setState(0);  // 逻辑删除
        int result = vipCardService.update(vipCard);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 查询单个信息
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/findOneVipCard")
    @ResponseBody
    public Object findOneVipCard(VipCard vipCard) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        VipCard vip = vipCardService.findOne(vipCard);
        if (vip != null) {
            map.put("vip", vip);
        }
        return map;
    }

    /**
     * 查询信息
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @return
     */
    @RequestMapping("/findVipCard")
    @ExceptionHandler(value = StatusException.class)
    public String findVipCard(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {

        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5);
        VipCard vipCard = new VipCard();
        vipCard.setState(1);
        List<VipCard> vipCardList = vipCardService.findVipCard(vipCard);

        Branches branches = new Branches();
        branches.setState(1);
//        List<Branches> branchesList = branchesService.findAll(branches);

        PageInfo pageInfo = new PageInfo(vipCardList);

//        List<BranchesDto> branchesDtoList = VipCardDtoUtil.getVipCardDto(pageInfo.getList(), branchesList);
//        pageInfo.setList(branchesDtoList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "vipCardEntry";
    }

}
